﻿using System.Data;

using SqlExecHelper.Config;
namespace SqlExecHelper
{
        public class SqlBasicDAL
        {
                private readonly string _ConName;
                internal ITransaction CurrentTran = null;
                public int RowNum => SqlHelper.RowNum;

                protected internal IDAL MyDAL => this._GetDAL ();

                public SqlBasicDAL ()
                {

                }
                public SqlBasicDAL (string conName)
                {
                        this._ConName = conName;
                }
                private IDAL _GetDAL ()
                {
                        if (this._ConName == null)
                        {
                                return SqlConfig.SqlFactory.GetDAL ();
                        }
                        else
                        {
                                return SqlConfig.SqlFactory.GetDAL (this._ConName);
                        }
                }
                #region 事务
                protected IDAL BeginTrans ()
                {
                        return this.MyDAL.BeginTrans ();
                }

                protected void CommitTrans ()
                {
                        this.MyDAL.CommitTrans ();
                }
                protected void RollbackTrans ()
                {
                        this.MyDAL.RollbackTrans ();
                }
                #endregion

                #region 执行SQL

                protected IBatchExecSql BatchSql (int row, int col)
                {
                        return SqlExecTool.BatchSql (this.MyDAL, row, col);
                }
                protected bool ExecuteNonQuery (string sql, params SqlBasicParameter[] param)
                {
                        return SqlExecTool.ExecuteNonQuery (sql, this.MyDAL, param);
                }

                protected T ExecuteScalar<T> (string sql, params SqlBasicParameter[] param)
                {
                        return SqlExecTool.ExecuteScalar<T> (sql, this.MyDAL, param);
                }
                protected T GetRow<T> (string sql, params SqlBasicParameter[] param)
                {
                        return SqlExecTool.GetRow<T> (sql, this.MyDAL, param);
                }
                protected T[] GetTable<T> (string sql, params SqlBasicParameter[] param)
                {
                        return SqlExecTool.GetTable<T> (sql, this.MyDAL, param);
                }
                protected DataSet GetDataSet (string sql, params SqlBasicParameter[] param)
                {
                        return SqlExecTool.GetDataSet (sql, this.MyDAL, param);
                }
                #endregion
        }
}
